import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Welcome from '../components/Welcome.vue'
import Home from '../components/Home.vue'
import User from '../components/items/User.vue'
import PermissionManagement from '../components/items/PermissionManagement.vue'
import Roles from '../components/items/Roles.vue'
import Goods from '../components/items/Goods.vue'
import Params from '../components/items/Params.vue'
import Categories from '../components/items/Categories.vue'
import Orders from '../components/items/Orders.vue'
import Reports from '../components/items/Reports.vue'
import NotFoundComponent from '../components/items/NotFoundComponent.vue'
import ChangeGood from '../components/items/ChangeGood.vue'
Vue.use(VueRouter)

const routes = [
  { path: '*', component: NotFoundComponent },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    children: [
      {
        path: '',
        redirect: '/welcome'
      },
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: 'users',
        component: User
      },
      {
        path: 'rights',
        component: PermissionManagement
      },
      {
        path: 'roles',
        component: Roles
      },
      {
        path: 'goods',
        component: Goods
      },
      {
        path: 'params',
        component: Params
      },
      {
        path: 'categories',
        component: Categories
      },
      {
        path: 'orders',
        component: Orders
      },
      {
        path: 'reports',
        component: Reports
      },
      {
        name: 'good',
        path: 'changeGood',
        component: ChangeGood,
        props: true
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 添加路由导航守卫，拦截一下非法地址
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (token == null) return next('/login')
  next()
})
export default router
